package com.wanpinghui.supplier.business;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.adapter.WhoCanSeeAdapter;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.BusinessScopeActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.toast.ToastManager;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;
import com.yunshl.yunshllibrary.storage.ShareDataManager;
import com.yunshl.yunshllibrary.storage.SharedPreferencesKey;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/26.
 */
@ContentView(R.layout.layout_activity_who_can_see)
public class WhoCanSeeActivity extends BaseActivity {

    private WhoCanSeeAdapter adapter;

    @ViewInject(R.id.expandableListView)
    private ExpandableListView expandableListView;


    private List<AreaBean> areaBeanList;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        expandableListView.setGroupIndicator(null);
        expandableListView.setDivider(null);
        String data = ShareDataManager.getInstance().getPara(SharedPreferencesKey.SELECT_WHO_CAN_SEE);
        if (TextUtil.isEmpty(data)) {
            areaBeanList = new ArrayList<>();
            areaBeanList.add(new AreaBean("仅好友可见", true));
            areaBeanList.add(new AreaBean("仅自己可见", false));
            areaBeanList.add(new AreaBean("全国工程商可见", false));
            areaBeanList.add(new AreaBean("部分工程商可见", false));
            getAreaEngineerInfo();
    }else {
            areaBeanList = new Gson().fromJson(data, new TypeToken<List<AreaBean>>() {}.getType());
            adapter = new WhoCanSeeAdapter(areaBeanList,WhoCanSeeActivity.this);
            expandableListView.setAdapter(adapter);
            if (adapter.getData().get(3).isSelect()){
                expandableListView.expandGroup(3);
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void bindEvents() {
        findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_release).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = null;
                int type = 1;
            //    int bid_point = 0;
              ArrayList<String> area_idsList = new ArrayList<String>();
                for (AreaBean bean : adapter.getData()){
                    if (bean.isSelect()){
                          name =   bean.getName();
                        if (name.contains("部分")){
                            if (bean.getChildren() != null){
                                for (AreaBean bean1 : bean.getChildren()){
                                    if (bean1.isSelect()){
                                        area_idsList.add(String.valueOf(bean1.getId()));
                                   //    bid_point = bid_point + bean1.getBid_point();
                                    }
                                }
                            }
                            type = 31;
                        }else if (name.contains("好友")) {
                            type = 1;
                        }else if (name.contains("全国")){
                     //       bid_point = bean.getEngineer_quantity() - bean.getFirend_quantity();
                            type = 21;
                        }else {
                            type = 11;
                        }
                    }
                }

                Intent intent = new Intent();

                //visible_type (integer, optional): 可见类型:1好友可见/11仅自己可见/21全国可见/31部分可见
                intent.putExtra("visible_type",type);
           //     intent.putExtra("bid_point",bid_point);
                if (area_idsList.size()>0) {
                    intent.putExtra("area_ids", area_idsList.toString().substring(1,area_idsList.toString().length()-1));
                }
                intent.putExtra("data",name);
                setResult(RESULT_OK,intent);
                ShareDataManager.getInstance().save(WhoCanSeeActivity.this, SharedPreferencesKey.SELECT_WHO_CAN_SEE, new Gson().toJson(adapter.getData()));
                finish();
            }
        });

    }

    @Override
    public void initData() {

    }

    private void getAreaEngineerInfo() {
        WPHSdk.getService(BusinessService.class).getAreaEngineerInfo(new YRequestCallback<List<AreaBean>>() {
            @Override
            public void onSuccess(List<AreaBean> var1) {

                if (areaBeanList != null && var1 != null && var1.size()>0){

                    areaBeanList.get(3).setSubs(var1);
                    int engineer_quantity = 0 ;
                    int firend_quantity = 0;
                    for (AreaBean bean : var1){
                        engineer_quantity = engineer_quantity + bean.getEngineer_quantity();
                        firend_quantity = firend_quantity +bean.getFirend_quantity();
                    }
                    areaBeanList.get(2).setEngineer_quantity(engineer_quantity);
                    areaBeanList.get(2).setFirend_quantity(firend_quantity);
                }
                adapter = new WhoCanSeeAdapter(areaBeanList,WhoCanSeeActivity.this);
                expandableListView.setAdapter(adapter);

            }

            @Override
            public void onFailed(int var1, String message) {

            }

            @Override
            public void onException(Throwable var1) {

            }
        });
    }


}
